local skel = fk.CreateSkill{
  name = "tea__xuanfeng_active&",
}
Fk:loadTranslationTable{
  ["tea__xuanfeng_active&"] = "悬锋",
  [":tea__xuanfeng_active&"] = "你可以令场上有“悬锋”【闪电】的角色进行【闪电】判定，然后你视为使用一张【杀】。。",
  ["#tea__xuanfeng_active"] = "悬锋：你可以令场上有“悬锋”【闪电】的角色进行【闪电】判定，然后你视为使用一张【杀】。",
}
---@type ViewAsSkillSpec
local xuanfeng_active = {
  mute = true,
  pattern = "slash",
  prompt = "#tea__xuanfeng_active",
  card_filter = function (self, player, to_select, selected)
    return false
  end,
  view_as = function (self, player, cards)
    if #cards ~= 0 then return end
    local c = Fk:cloneCard("slash")
    c.skillName = skel.name
    return c
  end,
  after_use = function (self, player, use)
    local room = player.room
    local cid = 0
    local to
    for _, p in ipairs(room:getAlivePlayers()) do
      for _, id in ipairs(p:getCardIds("j")) do
        if #Fk:getCardById(id):getTableMark("@@tea__xuanfeng-inarea") > 0 then
          cid = id
          to = p
        end
      end
    end
    if cid ~= 0 then
      local card = to:getVirualEquip(cid)
      if not card then return skel.name end
      if to:hasSkill("tea__xuanfeng", true) then
        room:notifySkillInvoked(to, "tea__xuanfeng", "special")
        to:broadcastSkillInvoke("tea__xuanfeng", 1)
      else
        room:notifySkillInvoked(player, "tea__xuanfeng", "special")
        player:broadcastSkillInvoke("tea__xuanfeng", 2)
      end
      room:moveCardTo(card, Card.Processing, nil, fk.ReasonUse, "lightning_skill")
      local judge = {
        who = to,
        reason = "lightning",
        pattern = ".|2~9|spade",
      }
      room:judge(judge)
      if judge:matchPattern() then
        room:damage{
          to = to,
          damage = 3,
          damageType = fk.ThunderDamage,
          skillName = skel.name,
        }
        room:moveCardTo(card, Card.DiscardPile, nil, fk.ReasonUse, "lightning_skill")
      else
        local nextp = to
        repeat
          nextp = nextp:getNextAlive(true)
          if nextp == to then
            if nextp:isProhibited(nextp, card) then
              room:moveCards{
                ids = room:getSubcardsByRule(card, { Card.Processing }),
                toArea = Card.DiscardPile,
                moveReason = fk.ReasonPut,
              }
              return
            end
            break
          end
        until not nextp:hasDelayedTrick("lightning") and not nextp:isProhibited(nextp, card)


        if card:isVirtual() then
          nextp:addVirtualEquip(card)
        end

        room:moveCardTo(card, Card.PlayerJudge, nextp, fk.ReasonPut, "lightning_skill")
      end
    end
  end,
  enabled_at_play = function (self, player)
    local to
    if table.find(Fk:currentRoom().alive_players, function (p)
      ---@diagnostic disable-next-line: return-type-mismatch
      return table.find(p:getCardIds("j"), function (id)
        if #Fk:getCardById(id):getTableMark("@@tea__xuanfeng-inarea") > 0 then
          to = p
          return true
        end
      end)
    end) then
      if to:hasSkill("tea__xuanfeng", true) then
        return not player:hasSkill("tea__xuanfeng", true)
      else
        return player:hasSkill("tea__xuanfeng", true)
      end
    end
  end,
  enabled_at_response = function (self, player, response)
    local to
    if not response and table.find(Fk:currentRoom().alive_players, function (p)
      ---@diagnostic disable-next-line: return-type-mismatch
      return table.find(p:getCardIds("j"), function (id)
        if #Fk:getCardById(id):getTableMark("@@tea__xuanfeng-inarea") > 0 then
          to = p
          return true
        end
      end)
    end) then
      if to:hasSkill("tea__xuanfeng", true) then
        return not player:hasSkill("tea__xuanfeng", true)
      else
        return player:hasSkill("tea__xuanfeng", true)
      end
    end
  end
}
skel:addEffect("viewas", xuanfeng_active)
return skel